import requests
import re
import pymysql
import json

def spider_map_data():
    db = pymysql.connect(host="119.23.40.47", user="xxx", passwd="xxx", db="cov19", charset="utf8")
    cursor = db.cursor()
    url = 'https://view.inews.qq.com/g2/getOnsInfo?name=disease_h5&_=1625303963699'
    res = requests.get(url).text
    text = res.replace('\\', '')
    pat = '"data":"(.*?)"}'
    text = re.compile(pat).findall(text)
    jl = json.loads(text[0])
    cursor.execute('truncate table proSit;')
    count = 0
    for i in range(0, 34):
        result = jl['areaTree'][0]['children'][i]
        name = result['name']
        nowConfirm = result['total']['nowConfirm']
        confirm = result['total']['confirm']
        sql = f"INSERT INTO proSit(id,name,nowConfirm, confirm) VALUES ({count},'{name}',{nowConfirm},{confirm});"
        count += 1
        cursor.execute(sql)
        db.commit()
    db.close()